package com.example.widget_lib.widget.progress.circle;

import com.example.widget_lib.utils.PxUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class GradientProBar extends Component implements Component.DrawTask {

    // 圆弧线宽
    private float mCircleBorderWidth = PxUtil.vp2px(20);
    // 内边距
    private float mCirclePadding = PxUtil.vp2px(20);
    // 字体大小
    private float mTextSize = PxUtil.fp2px(50);
    // 绘制圆周的画笔
    private Paint mBackCirclePaint;
    // 绘制圆周白色分割线的画笔
    private Paint mLinePaint;
    // 绘制文字的画笔
    private Paint mTextPaint;
    // 绘制渐变效果画笔
    private Paint mGradientCirclePaint;
    // 百分比
    private int mPercent = 0;
    // 渐变圆周颜色数组
    private Color[] mGradientColorArray = new Color[]{
            Color.GREEN,
            new Color(Color.getIntColor("#fe751a")),
            new Color(Color.getIntColor("#13be23")),
            Color.GREEN
    };

    public GradientProBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
        addDrawTask(this);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    private void init() {
        mBackCirclePaint = new Paint();
        mBackCirclePaint.setStyle(Paint.Style.STROKE_STYLE);
        mBackCirclePaint.setAntiAlias(true);
        mBackCirclePaint.setColor(Color.LTGRAY);
        mBackCirclePaint.setStrokeWidth(mCircleBorderWidth);

        mGradientCirclePaint = new Paint();
        mGradientCirclePaint.setStyle(Paint.Style.STROKE_STYLE);
        mGradientCirclePaint.setAntiAlias(true);
        mGradientCirclePaint.setColor(Color.LTGRAY);
        mGradientCirclePaint.setStrokeWidth(mCircleBorderWidth);

        mLinePaint = new Paint();
        mLinePaint.setColor(Color.WHITE);
        mLinePaint.setStrokeWidth(5);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize((int) mTextSize);
        mTextPaint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        // 1.绘制灰色背景圆环
        canvas.drawArc(
                new RectFloat(mCirclePadding, mCirclePadding,
                        getWidth() - mCirclePadding, getHeight() - mCirclePadding)
                , new Arc(-90
                        , 360
                        , false)
                , mBackCirclePaint);
        // 2.绘制颜色渐变圆环
        Point[] points = new Point[]{new Point(mCirclePadding, mCirclePadding), new Point(getWidth() - mCirclePadding, getHeight() - mCirclePadding)};
        LinearShader shader = new LinearShader(points, null, mGradientColorArray, Shader.TileMode.MIRROR_TILEMODE);
        mGradientCirclePaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        canvas.drawArc(
                new RectFloat(mCirclePadding, mCirclePadding,
                        getWidth() - mCirclePadding, getHeight() - mCirclePadding)
                , new Arc(-90
                        , (float) (mPercent / 100.0) * 360
                        , false)
                , mGradientCirclePaint);
        // 半径
        float radius = (getWidth() - mCirclePadding) / 2;
        // x轴中点坐标
        int centerX = getHeight() / 2;
        // 3.绘制100份线段，切分空心圆弧
        for (int i = 0; i < 360; i++) {
            double rad = i * Math.PI / 100;
            float startX = (float) (centerX + (radius - mCircleBorderWidth) * Math.sin(rad));
            float startY = (float) (centerX + (radius - mCircleBorderWidth) * Math.cos(rad));
            float stopX = (float) (centerX + radius * Math.sin(rad) + 2);
            float stopY = (float) (centerX + radius * Math.cos(rad) + 2);
            canvas.drawLine(new Point(startX, startY), new Point(stopX, stopY), mLinePaint);
        }
        // 4.绘制文字
        float textWidth = mTextPaint.measureText(mPercent + "%");
        int textHeight = (int) (Math.ceil(mTextPaint.getFontMetrics().descent - mTextPaint.getFontMetrics().ascent) + 2);
        canvas.drawText(mTextPaint, mPercent + "%", centerX - textWidth / 2, centerX + textHeight / 4);
    }

    /**
     * 设置百分比
     *
     * @param percent
     */
    public void setPercent(int percent) {
        if (percent < 0) {
            percent = 0;
        } else if (percent > 100) {
            percent = 100;
        }
        this.mPercent = percent;
        invalidate();
    }
}